package com.gs.common.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 1.定时校验来解决
 * 每隔2秒校验异常lettuce连接是否正常，解决长期空闲lettuce连接关闭但是netty不能及时监控到的问题
 * csdn: https://blog.csdn.net/qzy___18201245790/article/details/119168026
 * @author hujun
 */
@Component
@Slf4j
@EnableScheduling
public class LettuceConnectionValidTask   {

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @Scheduled(cron="0/2 * * * * ?")
    public void task() {
//        log.info("每隔2秒校验异常lettuce连接是否正常，解决长期空闲lettuce连接关闭但是netty不能及时监控到的问题");
        if(redisConnectionFactory instanceof LettuceConnectionFactory){
            LettuceConnectionFactory c=(LettuceConnectionFactory)redisConnectionFactory;
            c.validateConnection();
        }
    }
}
